package com.shenchuangit.webprintclient.util;

import javax.print.PrintService;
import java.awt.print.PrinterJob;

public class PrintHelper {
    public static PrintService getPrintServiceLikeName(String printerName) {
        PrintService printService = null;
        if (printerName != null) {
            //获得本台电脑连接的所有打印机
            PrintService[] printServices = PrinterJob.lookupPrintServices();
            if (printServices == null || printServices.length == 0) {
                System.out.print("打印失败，未找到可用打印机，请检查。");
                return null;
            }
            //匹配指定打印机
            for (PrintService service : printServices) {
                System.out.println(service.getName());
                if (service.getName().contains(printerName)) {
                    printService = service;
                    break;
                }
            }
            if (printService == null) {
                System.out.print("打印失败，未找到名称为" + printerName + "的打印机，请检查。");
                return null;
            }
        }

        return printService;
    }
}
